import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {PageHeaderType} from '../../../../share/components/page-header/page-header.component';
import {ActivatedRoute} from '@angular/router';
import {WindowService} from '../../../../core/services/common/window.service';
import {ExamAnswer} from '../../../../core/services/http/exams-manage/start-exam.service';


@Component({
  selector: 'app-result',
  templateUrl: './result.component.html',
  styleUrls: ['./result.component.less']
})
export class ResultComponent implements OnInit {
  pageHeaderInfo: Partial<PageHeaderType> = {
    breadcrumb: ['在线考试', '考试结果']
  };
  informRadio: ExamAnswer[] = [];
  informJudge: ExamAnswer[] = [];
  informultip: ExamAnswer[] = [];
  timeDate!: string;
  nameTitle = '';
  useTime = 0;
  score = 0;
  userNameSession = '';
  radioAnswer: any[] = [];
  judgeAnswer: any[] = [];
  multipAnswer: any[] = [];
  standardAnswer1: string[] = [];
  standardAnswer2: string[] = [];
  standardAnswer3: string[] = [];


  constructor(private routInfo: ActivatedRoute, private windowServe: WindowService, private cdr: ChangeDetectorRef) {
  }

  getCurrentDate(): void {
    this.timeDate = new Date().toLocaleString();
  }

  setMultipAnser(): void {
    this.informultip.forEach((optionItem, index) => {
      optionItem.questionOptionVos.forEach(selItem => {
        (selItem as any).label = selItem.content;
        (selItem as any).value = selItem.sort;
        if (this.multipAnswer[index].includes(selItem.sort)) {
          (selItem as any).checked = true;
        }
      });
    });
  }

  onInformation(): void {
    /*问题集*/
    this.informRadio = JSON.parse(this.windowServe.getStorage('radioArray')!);
    this.informJudge = JSON.parse(this.windowServe.getStorage('judgmentArray')!);
    this.informultip = JSON.parse(this.windowServe.getStorage('multipleArray')!);
    /*所选答案*/
    this.radioAnswer = JSON.parse(this.windowServe.getStorage('radioAnswer')!);
    this.judgeAnswer = JSON.parse(this.windowServe.getStorage('judgeAnswer')!);
    this.multipAnswer = JSON.parse(this.windowServe.getStorage('multipleAnswer')!);
    /*正确答案*/
    this.standardAnswer1 = JSON.parse(this.windowServe.getStorage('standardAnswer1')!);
    this.standardAnswer2 = JSON.parse(this.windowServe.getStorage('standardAnswer2')!);
    this.standardAnswer3 = JSON.parse(this.windowServe.getStorage('standardAnswer3')!);
    this.cdr.markForCheck();
  }

  ngOnInit(): void {
    this.onInformation();
    this.getCurrentDate();
    this.setMultipAnser();
    this.nameTitle = this.routInfo.snapshot.params['name'];
    this.score = this.routInfo.snapshot.params['score'];
    this.useTime = this.routInfo.snapshot.params['useTime'];
    this.userNameSession = JSON.parse(this.windowServe.getStorage('name')!);
  }


}
